package com.huiren.ui.importUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.annotation.PostConstruct;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.huiren.service.PurOrderService;

import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class NetImportJPanel extends JPanel {

	private static Logger logger = Logger.getLogger(NetImportJPanel.class);
	private static final long serialVersionUID = 1L;

	private Timer timer = null;
	private JTextArea result;

	@Autowired
	private PurOrderService purOrderService;

	public NetImportJPanel() {
		timer = new Timer(10000, new AbstractAction() {
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				try {
					List<String> ordernumbers = purOrderService.insertFromNet();
					for (String ordernumber : ordernumbers) {
						result.setText(result.getText()
								+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
										.format(new Date()) + " 已获取分录id:"
								+ ordernumber + "\n");
					}
				} catch (Exception e1) {
					logger.error(e1);
				}
			}
		});

		JPanel p = new JPanel();
		setLayout(new BorderLayout(0, 0));
		result = new JTextArea();
		result.setLineWrap(true);
		result.setEditable(false);
		this.add(new JScrollPane(result), BorderLayout.CENTER);
		this.add(p, BorderLayout.SOUTH);
		final JButton startButton = new JButton();
		p.add(startButton);
		startButton.setText("启动");
		final JButton stopButton = new JButton();
		p.add(stopButton);
		stopButton.setText("停止");
		stopButton.setEnabled(false);

		stopButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				timer.stop();
				((JButton) evt.getSource()).setEnabled(false);
				startButton.setEnabled(true);
			}
		});
		startButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				timer.start();
				((JButton) evt.getSource()).setEnabled(false);
				stopButton.setEnabled(true);
			}
		});
	}

	@PostConstruct
	public void initMethod() {

	}

}
